﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;

public partial class dangnhap : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataConnection.str = @"Data Source=xe;User ID=ODER;Password=12345";

    }
    protected void btnhuydn_Click(object sender, EventArgs e)
    {
        txtdusername1.Text = txtpasswd.Text = "";
    }
    protected void btndangnhap_Click(object sender, EventArgs e)
    {
        mahoa mk = new mahoa();
        string tendn = txtdusername1.Text;

        // encrypt password
        string pass = mk.mahoamk(txtpasswd.Text, true);
       
        OracleParameter[] ThamSo = new OracleParameter[]
        {
            new OracleParameter("p_tendn",tendn),
            new OracleParameter("p_matkhaudn",pass),
        };

        // get user by TenDN and MatKhauDN
        DataTable dt = DataConnection.GetDatatableByStoreProcedureNameCursor("NGOCTRAN.sp_getUserByTenDNMatKhauDN", ThamSo);

        // if correct both TenDN and MatKhauDN
        if (dt.Rows.Count > 0)
        {
            // Check role
            string quyen = dt.Rows[0][0].ToString();
            Session["TenDN"] = tendn;
            Session["Quyen"] = quyen;

            
            if (quyen == "0") //role: member
            {

                Response.Redirect("trangchu.aspx");
                if (CheckBox1.Checked)
                {
                    HttpCookie cookie = new HttpCookie("ckusername" + tendn + "~" + mk);
                    cookie.Expires = DateTime.Now.AddDays(2);
                    Response.Cookies.Add(cookie);
                }
                

            }
            else
            {
                if (quyen == "1") //role: admin
                {
                    Session["admin"] = "admin";
                    Response.Redirect("Admin/admin.aspx");
                }
            }
        }
        // not correct TenDN and password.
        else
        {
            Response.Redirect("~/thongbaoloi.aspx?ID=1");
        }
    }

    protected void txtpasswd_TextChanged(object sender, EventArgs e)
    {
        btndangnhap_Click(sender, e);
    }
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

    }
}